package com.example.my;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link Blank2Fragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class Blank2Fragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    OkHttpClient okHttpClient = new OkHttpClient();
    private TextView textView16;
    private LinearLayout linearLayout;
    private TextView textView0;
    private TextView textView1;
    private LinearLayout linearLayout4;
    private TextView textView7;
    private TextView textView9;
    private LinearLayout linearLayout5;
    private TextView textView15;
    private LinearLayout linearLayout7;
    private TextView textView18;
    private TextView textView19;
    private LinearLayout linearLayout8;
    private TextView textView20;
    private TextView textView21;
    private LinearLayout linearLayout9;
    private TextView textView22;
    private TextView textView23;
    private LinearLayout linearLayout10;
    private TextView textView24;
    private TextView textView25;
    private LinearLayout linearLayout11;
    private TextView textView26;
    private TextView textView27;
    private LinearLayout linearLayout12;
    private TextView textView28;
    private TextView textView29;
    private LinearLayout linearLayout13;
    private TextView textView30;
    private TextView textView31;

    public Blank2Fragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment Blank2Fragment.
     */
    // TODO: Rename and change types and number of parameters
    public static Blank2Fragment newInstance(String param1, String param2) {
        Blank2Fragment fragment = new Blank2Fragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_blank2, container, false);
        initView(view);
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                Request request = new Request.Builder()
                        .url("https://api.jisuapi.com/movie/theater?cityid=&city=佛山&keyword=&appkey=0b99a4583284f5c2")
                        .build();
                Call call = okHttpClient.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(@NotNull Call call, @NotNull IOException e) {

                    }

                    @Override
                    public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                        String result = response.body().string();
                        getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Gson gson = new Gson();
                                Movie movie = gson.fromJson(result, Movie.class);
//                                Log.i("TAG", "run: " + result);
                                textView0.setText(movie.getResult().getList().get(10).getTheatername());
                                textView1.setText(movie.getResult().getList().get(10).getAddress());
                                textView7.setText(movie.getResult().getList().get(1).getTheatername());
                                textView9.setText(movie.getResult().getList().get(1).getAddress());
                                textView15.setText(movie.getResult().getList().get(12).getTheatername());
                                textView16.setText(movie.getResult().getList().get(12).getAddress());
                                textView18.setText(movie.getResult().getList().get(3).getTheatername());
                                textView29.setText(movie.getResult().getList().get(3).getAddress());
                                textView20.setText(movie.getResult().getList().get(4).getTheatername());
                                textView21.setText(movie.getResult().getList().get(4).getAddress());
                                textView22.setText(movie.getResult().getList().get(5).getTheatername());
                                textView23.setText(movie.getResult().getList().get(5).getAddress());
                                textView24.setText(movie.getResult().getList().get(6).getTheatername());
                                textView25.setText(movie.getResult().getList().get(6).getAddress());
                                textView26.setText(movie.getResult().getList().get(7).getTheatername());
                                textView27.setText(movie.getResult().getList().get(7).getAddress());
                                textView28.setText(movie.getResult().getList().get(8).getTheatername());
                                textView29.setText(movie.getResult().getList().get(8).getAddress());
                                textView30.setText(movie.getResult().getList().get(2).getTheatername());
                                textView31.setText(movie.getResult().getList().get(2).getAddress());

                            }
                        });

                    }
                });

            }
        }, 0, 600000);
        return view;
    }

    private void initView(View view) {
        textView0 = (TextView) view.findViewById(R.id.textView0);
        textView1 = (TextView) view.findViewById(R.id.textView1);
        textView7 = (TextView) view.findViewById(R.id.textView7);
        textView9 = (TextView) view.findViewById(R.id.textView9);
        textView15 = (TextView) view.findViewById(R.id.textView15);
        textView16 = (TextView) view.findViewById(R.id.textView16);
        textView18 = (TextView) view.findViewById(R.id.textView18);
        textView19 = (TextView) view.findViewById(R.id.textView19);
        textView20 = (TextView) view.findViewById(R.id.textView20);
        textView21 = (TextView) view.findViewById(R.id.textView21);
        textView22 = (TextView) view.findViewById(R.id.textView22);
        textView23 = (TextView) view.findViewById(R.id.textView23);
        textView24 = (TextView) view.findViewById(R.id.textView24);
        textView25 = (TextView) view.findViewById(R.id.textView25);
        textView26 = (TextView) view.findViewById(R.id.textView26);
        textView27 = (TextView) view.findViewById(R.id.textView27);
        textView28 = (TextView) view.findViewById(R.id.textView28);
        textView29 = (TextView) view.findViewById(R.id.textView29);
        textView30 = (TextView) view.findViewById(R.id.textView30);
        textView31 = (TextView) view.findViewById(R.id.textView31);
    }
}